<script setup>
import { ref } from 'vue'
import { listMemberCarService, deleteCurrentMemberCarService } from '@/api/car.js'
import UserCarEdit from './components/UserCarEdit.vue'

// 下拉刷新
const refreshLoading = ref(false)

// 车辆列表
const carList = ref()

const listMemberCar = async () => {
  const res = await listMemberCarService()
  carList.value = res.data.data
  refreshLoading.value = false
}
listMemberCar()

// 删除车辆
const handleDelete = (id) => {
  showDialog({
    title: '确认删除',
    message: '确定要删除这辆车吗？',
    showConfirmButton: true,
    showCancelButton: true
  }).then(async () => {
    await deleteCurrentMemberCarService(id)
    showNotify({
      type: 'success',
      message: '删除成功'
    })
    listMemberCar()
  })
}

// 车牌号格式化
const formatPlateNumber = (plateNumber) => {
  if (plateNumber.length < 3) return plateNumber
  const province = plateNumber.slice(0, 2)
  const rest = plateNumber.slice(2)
  return `${province}•${rest}`
}

// 获取车牌底色
const getPlateNumberBgc = (plateNumber) => {
  // 新能源车牌（绿底）
  if (plateNumber.length === 8)
    return {
      'background-image': 'linear-gradient(to bottom, #F6F7F9 0%, #21C479 100%)'
    }
  // 挂车、教练车（黄底）
  if (/挂|学/.test(plateNumber)) return { 'background-color': '#f1bf00' }
  // 警车（白底）
  if (/警/.test(plateNumber)) return { 'background-color': '#fff' }
  // 港、澳车（黑底）
  if (/港|澳/.test(plateNumber)) return { 'background-color': '#000' }
  // 其他（蓝底）
  return { 'background-color': '#0041c5' }
}

// 获取车牌字体颜色
const getPlateNumberFontColor = (plateNumber) => {
  // 黑字（新能源车牌、挂车、教练车、警车）
  if (plateNumber.length === 8 || /挂|学|警/.test(plateNumber)) return '#000'
  // 白字（港、澳车、蓝牌）
  return '#fff'
}

// 获取车牌边框颜色
const getPlateNumberBorderColor = (plateNumber) => {
  // 黑边框（新能源车牌、挂车、教练车、警车）
  if (plateNumber.length === 8 || /挂|学|警/.test(plateNumber)) return '3px solid #000'
  // 白边框（港、澳车、蓝牌）
  return '3px solid #fff'
}

const plateNumberEditRef = ref()
// 新增逻辑
const handleAdd = () => {
  plateNumberEditRef.value.open({})
}
// 编辑逻辑
const handleEdit = (row) => {
  plateNumberEditRef.value.open(row)
}
</script>

<template>
  <div class="car-manage">
    <van-nav-bar title="车辆管理" left-text="返回" left-arrow @click-left="$router.back()" fixed />
    <van-pull-refresh v-model="refreshLoading" @refresh="listMemberCar" success-text="加载成功">
      <div class="user-car">
        <van-empty v-if="!carList?.length" image="search" description="无车辆信息" />
        <!-- 车辆列表 -->
        <van-swipe-cell v-for="car in carList" :key="car.id">
          <van-cell :border="false">
            <template #title>
              <div
                class="plate-number"
                :style="{
                  ...getPlateNumberBgc(car.plateNumber),
                  border: getPlateNumberBorderColor(car.plateNumber)
                }"
              >
                <van-password-input
                  :value="formatPlateNumber(car.plateNumber)"
                  :gutter="10"
                  :mask="false"
                  :length="formatPlateNumber(car.plateNumber).length"
                  :style="{ '--plateNumber-color': getPlateNumberFontColor(car.plateNumber) }"
                />
              </div>
              <div class="car-info">
                <span class="brand">{{ car.brand }}</span>
                |
                <span class="model">{{ car.model }}</span>
              </div>
            </template>
          </van-cell>

          <template #right>
            <van-button
              square
              text="编辑"
              type="primary"
              class="operate-button"
              @click="handleEdit(car)"
            />
            <van-button
              square
              text="删除"
              type="danger"
              class="operate-button"
              @click="handleDelete(car.id)"
            />
          </template>
        </van-swipe-cell>

        <van-button type="primary" block round @click="handleAdd()">新增车辆</van-button>

        <user-car-edit ref="plateNumberEditRef" @success="listMemberCar"></user-car-edit>
      </div>
    </van-pull-refresh>
  </div>
</template>

<style lang="scss" scoped>
.car-manage {
  padding-top: 46px;
  background-color: #f7f8fa;
  min-height: 100vh;

  .user-car {
    height: 100%;

    .van-cell {
      background-color: #fff;
      margin: 10px 0px;

      .plate-number {
        font-size: 20px;
        border-radius: 8px;
        padding: 10px 15px;
        margin-bottom: 8px;
        box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.4);

        :deep(.van-password-input__security) {
          --van-password-input-height: 30px;

          .van-password-input__item {
            color: var(--plateNumber-color);
            background-color: rgba($color: #fff, $alpha: 0);
          }
        }
      }

      .car-info {
        color: #333;
        line-height: 1.4;
        font-size: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
        padding-left: 10px;

        .brand {
          font-size: 18px;
          font-weight: 700;
          color: #000;
        }
        .model {
          line-height: 16px;
        }
      }
    }

    .operate-button {
      height: 100%;
    }
  }
}
</style>
